//
//  HMEmoticonToolBar.swift
//  Weibo
//
//  Created by fuyu on 2017/8/4.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
//  表情类型
enum HMEmoticonToolBarButtonType: Int {
    //  最近表情
    case recent = 1000
    //  默认表情
    case normal = 1001
    //  emoji
    case emoji = 1002
    //  浪小花
    case lxh = 1003
}
//  表情键盘toolbar
class HMEmoticonToolBar: UIStackView {
    //  记录上一次选中的按钮
    var lastSelectedButton: UIButton?
    //  点击toolbar按钮执行的闭包
    var clickToolBarButtonClosure: ((HMEmoticonToolBarButtonType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        self.axis = .horizontal
        //  等比填充
        self.distribution = .fillEqually
        addChildButton(title: "最近", imageName: "compose_emotion_table_left", type: .recent)
        addChildButton(title: "默认", imageName: "compose_emotion_table_mid", type: .normal)
        addChildButton(title: "Emoji", imageName: "compose_emotion_table_mid", type: .emoji)
        addChildButton(title: "浪小花", imageName: "compose_emotion_table_right", type: .lxh)
    }
    
    //  添加子按钮
    private func addChildButton(title: String, imageName: String, type: HMEmoticonToolBarButtonType) {
        let button = UIButton()
        //  枚举的原始值作为tag使用
        button.tag = type.rawValue
        //  添加点击事件
        button.addTarget(self, action: #selector(buttonAction(btn: )), for: .touchUpInside)
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.gray, for: .selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: imageName + "_selected"), for: .selected)
        //  取消高亮
        button.adjustsImageWhenHighlighted = false
        //  添加控件
        addArrangedSubview(button)
        if type == .normal {
            lastSelectedButton?.isSelected = false
            button.isSelected = true
            lastSelectedButton = button
        }
    }
    //  MARK: --toolbar按钮点击事件处理
    @objc private func buttonAction(btn: UIButton) {
        //  表示点击的是同一个按钮，不需要再次执行选中
        if lastSelectedButton == btn {
            return
        }
        //  上一次按钮取消选中
        lastSelectedButton?.isSelected = false
        //  点击的按钮选中
        btn.isSelected = true
        //  记录这次点击选中的按钮
        lastSelectedButton = btn
        //  根据tag获取枚举
        let type = HMEmoticonToolBarButtonType(rawValue: btn.tag)!
        //  执行闭包
        clickToolBarButtonClosure?(type)
    }
    //  根据表情组数索引选中对应的按钮
    func selectButtonWith(section: Int) {
        //  根据tag获取对于的子视图, 提示：视图的tag不建议从0开始设置，因为如果tag是0那么获取的视图是当前视图自己
        let button = viewWithTag(section + 1000) as! UIButton
        //  判断是否是同一个按钮
        if button == lastSelectedButton {
            return
        }
        print("哈哈")
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
        
    }
}

